<template>
  <div>
    <Category :scene="scene"></Category>

    <el-card style="margin: 10px 0px">
      <!-- v-if和v-show都可以实现显示与隐藏 -->
      <div v-show="scene == 0">
        <el-button
          type="primary"
          icon="Plus"
          :disabled="categoryStore.c3Id ? false : true"
          @click="addSpu"
        >
          添加SPU
        </el-button>
        <!-- 展示已有的spu数据 -->
        <el-table style="margin: 10px 0px" border :data="records">
          <el-table-column
            label="序号"
            type="index"
            align="center"
            width="80px"
          ></el-table-column>
          <el-table-column label="SPU名称" prop="spuName"></el-table-column>
          <el-table-column
            label="SPU描述"
            prop="description"
            show-overflow-tooltip
          ></el-table-column>
          <el-table-column label="SPU操作">
            <!--  $index -->
            <template #="{ row }">
              <el-button
                type="primary"
                size="small"
                icon="Plus"
                title="添加sku"
                @click="addSku(row)"
              ></el-button>
              <el-button
                type="primary"
                size="small"
                icon="Edit"
                title="修改spu"
                @click="updateSpu(row)"
              ></el-button>
              <el-button
                type="primary"
                size="small"
                icon="View"
                title="查看spu"
                @click="findSku(row)"
              ></el-button>

              <el-popconfirm
                :title="`确定删除${row.spuName}?`"
                width="200px"
                @confirm="deleteSpu(row)"
              >
                <template #reference>
                  <el-button
                    type="primary"
                    icon="Delete"
                    size="small"
                    title="删除spu"
                  ></el-button>
                </template>
              </el-popconfirm>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页器 -->
        <el-pagination
          v-model:current-page="pageNo"
          v-model:page-size="pageSize"
          :page-sizes="[3, 5, 7, 9]"
          :background="true"
          layout="prev, pager, next, jumper,->,sizes,total"
          :total="total"
          @current-change="getHasSpu"
          @size-change="changeSize"
        />
      </div>
      <!-- 添加修改spu -->
      <SpuForm
        ref="spuForm"
        v-show="scene == 1"
        @changeScene="changeScene"
      ></SpuForm>
      <!-- 添加修改sku -->
      <SkuForm
        ref="skuForm"
        v-show="scene == 2"
        @changeScene="changeScene"
      ></SkuForm>
      <!-- dialog对话框 展示已有的spu属性 -->
      <el-dialog title="sku列表" v-model="show">
        <el-table border :data="skuArr">
          <el-table-column label="sku名字" prop="skuName"></el-table-column>
          <el-table-column label="sku价格" prop="price"></el-table-column>
          <el-table-column label="sku重量" prop="weight"></el-table-column>
          <el-table-column label="sku图片">
            <!--  $index -->
            <template #="{ row }">
              <img
                :src="row.skuDefaultImg"
                style="width: 100px; height: 100px"
              />
            </template>
          </el-table-column>
        </el-table>
      </el-dialog>
    </el-card>
  </div>
</template>

<script setup lang="ts">
import SpuForm from './spuForm.vue'
import SkuForm from './skuForm.vue'
import type { SpuData } from '@/api/product/spu/type'
import { ref, watch, onBeforeUnmount } from 'vue'
import { reqHasSpu, reqSkuList, reqRemoveSpu } from '@/api/product/spu'
import type {
  HasSpuResponseData,
  Records,
  SkuInfoData,
  SkuData,
} from '@/api/product/spu/type'
//引入分类仓库
import useCategoryStore from '@/store/modules/category'
import { ElMessage } from 'element-plus'

let categoryStore = useCategoryStore()
//场景的数据
//0显示已有的spu，1添加修改已有spu，2添加sku的结构
let scene = ref<number>(0)
//分页器默认页码
let pageNo = ref<number>(1)
//每一页展示几条数据
let pageSize = ref<number>(3)
//存储已有的spu属性
let records = ref<Records>([])
//存储已有spu总数
let total = ref<number>(0)

//获取子组件实例spuForm
let spuForm = ref<any>()
//获取子组件实例skuForm
let skuForm = ref<any>()

//存储全部sku数据
let skuArr = ref<SkuData[]>([])

let show = ref<boolean>(false)

//监听3级分类id变化
watch(
  () => categoryStore.c3Id,
  () => {
    //务必保证有3级id
    if (!categoryStore.c3Id) return
    getHasSpu()
  },
)

//方法执行获取某一个3级分类下全部已有的spu
const getHasSpu = async (pager = 1) => {
  pageNo.value = pager
  let result: HasSpuResponseData = await reqHasSpu(
    pageNo.value,
    pageSize.value,
    categoryStore.c3Id,
  )
  if (result.code == 200) {
    records.value = result.data.records
    total.value = result.data.total
  }
}
//分页器下拉菜单发生变化触发
const changeSize = () => {
  getHasSpu()
}

//点击spu添加按钮回调
const addSpu = () => {
  //切换场景值
  scene.value = 1
  //调用子组件方法
  spuForm.value.initAddSpu(categoryStore.c3Id)
}

//修改已有的spu按钮回调
const updateSpu = (row: SpuData) => {
  //切换场景值
  scene.value = 1
  //调用子组件实例方法获取完整spu数据
  spuForm.value.initHasSpuData(row)
}

//子组件spuForm绑定的自定义事件，让子组件通知父组件切换场景为0
const changeScene = (obj: any) => {
  //子组件点击取消按钮变为场景0去展示已有spu
  scene.value = obj.flag
  if (obj.params == 'update') {
    //更新留在当前页
    getHasSpu(pageNo.value)
  } else {
    //添加留在第一页
    getHasSpu()
  }
}

//添加sku按钮的回调
const addSku = (row: SpuData) => {
  //点击添加按钮切换场景2
  scene.value = 2

  //调用子组件方法初始化添加sku数据
  skuForm.value.initSkuData(categoryStore.c1Id, categoryStore.c2Id, row)
}

//查看sku列表数据
const findSku = async (row: SpuData) => {
  let result: SkuInfoData = await reqSkuList(row.id as number)
  if (result.code == 200) {
    skuArr.value = result.data
    //对话框显示
    show.value = true
  }
}

//删除已有的spu按钮回调
const deleteSpu = async (row: SpuData) => {
  let result: SkuInfoData = await reqRemoveSpu(row.id as number)
  if (result.code == 200) {
    ElMessage({
      type: 'success',
      message: '删除成功!',
    })
    //获取剩余的spu数据
    getHasSpu(records.value.length > 1 ? pageNo.value : pageNo.value - 1)
  } else {
    ElMessage({
      type: 'error',
      message: '删除失败!',
    })
  }
}
//路由组件销毁后，把仓库相关数据清空
onBeforeUnmount(() => {
  categoryStore.$reset()
})
</script>

<style scoped></style>
